<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-04-12
 */
class Absence{
	
	static public $FLAG_INACTIVE = 0;
	
	static public $FLAG_UNACTIVE = 10;
	
	static public $TABLE_NAME="absence";

	static public function Create($staffId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute) {
		$tmp['staff_id'] = $staffId;
		$startTime = mktime($startHour, $startMinute, 0, $month, $day, $year);
		$endTime = mktime($endHour, $endMinute, 0, $month, $day, $year);
		
		$tmp["start_time"] = $startTime;
		$tmp["end_time"] = $endTime;
		
		$work_schedule = self::GetByYMD($staffId, $year, $month, $day);
		if( $work_schedule != null ){
			$tmp['id'] = $work_schedule['id'];
			self::Update($tmp);
			self::DecOrderLimitCount($year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, $staffId);
			return 0;
		}
		
		$tmp['create_time'] = time();
		
		$tmp['id'] = DB::Insert(self::$TABLE_NAME, $tmp);
		self::DecOrderLimitCount($year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, $staffId);
		return $tmp['id'];
	}
	
	static public function Update($deviceUse) {
		$work_schedule = self::GetByYMD($deviceUse['staff_id'], $deviceUse['year'], $deviceUse['month'], $deviceUse['day']);
		self::IncOrderLimitCount( $work_schedule );
		Table::UpdateCache(self::$TABLE_NAME, $deviceUse['id'], $deviceUse);
		
		return ;
	}
	static public function GetByYMD($staffId, $year, $month, $day) {
		$startTime = mktime(0, 0, 0, $month, $day, $year);
		$endTime = mktime(23, 59, 59, $month, $day, $year);
		if (!$year || !$month || !$day)
			return array();
		return DB::GetTableRow(self::$TABLE_NAME, array('staff_id' => $staffId, 'start_time >'.$startTime,'end_time < '.$endTime));
	}
	
	static public function GetByPeriod($year, $month, $day, $endDay, $staffId = 0) {
		
		if (!$year || !$month || !$day || !$endDay){
			return array();
		}
		$startTime = mktime(0, 0, 0, $month, $day, $year);
		$endTime = mktime(23, 59, 59, $month, $endDay, $year);
		//Logs::setLog("sdfffffffffffffffffffffffffffffffffffsssssssssssssssssss<>$year, $month, $day, $endDay<>$startTime, $endTime");
		$oc = array( 
			"start_time >$startTime",
			"end_time < $endTime"
		);
		if( $staffId == 0 ){
			return DB::LimitQuery(
				self::$TABLE_NAME, 
				array(
				'condition' => $oc,
				'order' => 'ORDER BY start_time ASC'
				));
		}
		$oc['staff_id'] = $staffId;
		return DB::LimitQuery(
				self::$TABLE_NAME, 
				array(
				'condition' => $oc,
				'order' => 'ORDER BY start_time ASC'
				));
	}
	
	static public function HasAbsence($startTime, $endTime, $staffId) {
		$sql = "select * from absence where staff_id=$staffId and !(start_time > $endTime or end_time < $startTime)";
		
		$temp = DB::GetQueryResult($sql, false);
		return $temp;
	}
	
	static public function IncOrderLimitCount($workSchedule){
		$staff = Staff::getById($workSchedule['staff_id']);
		$serviceIds = StaffSkill::GetSkillsByStaffId($workSchedule['staff_id']);
		
		$startLimit = $workSchedule['start_minute'] > 0 ? $workSchedule['start_hour'] + 1 : $workSchedule['start_hour'];
		
		$year = $workSchedule['year'];
		$month = $workSchedule['month'];
		$day = $workSchedule['day'];
		$endLimit = $workSchedule['end_hour'];
		if( $workSchedule['start_minute'] > 0 ){
			self::incOrderLimit($staff['shop_id'], $serviceIds, $year, $month, $day, $workSchedule['start_hour'], $workSchedule['start_minute'], $workSchedule['start_hour'] + 1, 0);
		}
		for( $index = $startLimit; $index < $endLimit; $index++ ){
			//Logs::setLog(" this time is <$index> <0> <".($index + 1)."> <0>  == 86");
			self::incOrderLimit($staff['shop_id'], $serviceIds, $year, $month, $day, $index, 0, $index + 1, 0);
		}
		
		if( $workSchedule['end_minute'] > 0 ){
			//Logs::setLog(" this time is <$endHour> <0> <$endHour> <$endMinute>  == 91");
			self::incOrderLimit($staff['shop_id'], $serviceIds, $year, $month, $day, $endLimit, 0, $endLimit, $workSchedule['end_minute']);
		}
	}
	
	static public function DecOrderLimitCount($year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, $staffId){
		$staff = Staff::getById($staffId);
		$serviceIds = StaffSkill::GetSkillsByStaffId($staffId);
		
		$startLimit = $startMinute > 0 ? $startHour + 1 : $startHour;
		$endLimit = $endHour;
		if( $startMinute > 0 ){
			//Logs::setLog(" this time is <$startHour> <$startMinute> <".($startHour + 1)."> <0>  == 82");
			self::decOrderLimit($staff['shop_id'], $serviceIds, $year, $month, $day, $startHour, $startMinute, $startHour + 1, 0);
		}
		for( $index = $startLimit; $index < $endLimit; $index++ ){
			//Logs::setLog(" this time is <$index> <0> <".($index + 1)."> <0>  == 86");
			self::decOrderLimit($staff['shop_id'], $serviceIds, $year, $month, $day, $index, 0, $index + 1, 0);
		}
		
		if( $endMinute > 0 ){
			//Logs::setLog(" this time is <$endHour> <0> <$endHour> <$endMinute>  == 91");
			self::decOrderLimit($staff['shop_id'], $serviceIds, $year, $month, $day, $endHour, 0, $endHour, $endMinute);
		}
	}
	
	static public function decOrderLimit($shopId, $serviceIds, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute){
		foreach($serviceIds as $k => $v){
			ShopOrderLimit::incOrderLimit($shopId, $v['service_id'], $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, -1);
		}
	}
	
	static public function incOrderLimit($shopId, $serviceIds, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute){
		foreach($serviceIds as $k => $v){
			ShopOrderLimit::incOrderLimit($shopId, $v['service_id'], $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, 1);
		}
	}
	
	static public function getRestTimeSum($staffId, $startTime, $endTime){
		$sql = "select sum(end_time-start_time) as sum_rest from absence where staff_id=$staffId and start_time > $startTime and end_time < $endTime";
		//Logs::setLog($sql);
		$temp = DB::GetQueryResult($sql, false);
		return $temp;
	}
	
	static public function getListByTime($staffId, $startTime, $endTime){
		$sql = "select * from absence where staff_id=$staffId and start_time >= $startTime and end_time <= $endTime order by start_time";
		//Logs::setLog($sql);
		$temp = DB::GetQueryResult($sql, true);
		return $temp;
	}
	
	static public function DeleteById($id){
		$sql = "delete from ".self::$TABLE_NAME." where id=$id" ;
		//Logs::setLog("new sql is || $sql ||");
		return DB::GetQueryResult($sql, false);
	}
	
}
